const app = getApp()
var sliderWidth = 70; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    loading: true,
    active: 0,
    tabs: [],
    lists: [],
    page: 0,
  },
  //监听页面加载
  onLoad: function () {
    let that = this;
    that.getcate();
  },
  //完成
  onReady: function () {
    let that = this;
    setTimeout(function () {
      that.setData({
        skeleton: false,
      })
    }, 600)
  },
  //下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      loading: true,
      lists: []
    });
    wx.showNavigationBarLoading();
    that.getitem();
    //停止下拉刷新
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
  },
  //上拉加载
  onReachBottom: function () {
    var that = this;
    that.setData({
      loading: true,
    });
    that.getitem();
  },
  //商品分类
  getcate: function () {
    let that = this;
    app.api().Get('api/v1/allwin-shop-cateLits', {cate_id: 0}, function (result) {
      if (result.code == 200){
        that.setData({
          tabs: result.data,
          cate_id: result.data[0].cate_id,
        });
        //开始计算滚动条位移
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              sliderLeft: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
            });
          }
        });
        that.getitem();
      }
    })
  },
  //点击请求数据
  getitem: function () {
    let that = this;
    var cate_id = that.data.cate_id;
    if (that.data.loading) {
      var page = that.data.page + 1;
      app.api().Get('api/v1/allwin-shop-manageItem', {
        cate_id: cate_id,
        page: page
      }, function (result) {
        if (result.code == 200) {
          var lists = that.data.lists;
          for (let i in result.data) {
            lists.push(result.data[i]);
          }
          that.setData({
            lists: lists,
            page: page,
          });
        }
        that.setData({
          loading: false,
        });
      })
    }
  },
  onChange(event) {
    let that = this;
    var tab = that.data.tabs
    var cate_id = that.data.tabs[event.detail.index]['cate_id'];
    that.setData({
      cate_id: cate_id,
      page: 0,
      loading: true,
      lists: [],
    });
    that.getitem();
  },
  shopEdit: function () {
    wx.navigateTo({
      url: 'shopedit?id=0',
    })
  }
})